package com.jkoss.wine.config.mvc;

import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.Date;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.jkoss.common.util.CommonMethod;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.util.CryptoUtils;

public class LogCostInterceptor implements HandlerInterceptor {
	long start = System.currentTimeMillis();
	Logger logger = Logger.getLogger(getClass());

	@Override
	public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
		Properties pro = new Properties();
		// 通过字节输入流
		// load(InputStream inStream)
		// pro.load(getClass().getResourceAsStream("/logcost.properties"));
		Reader reader = new FileReader("src" + File.separatorChar + "logcost.properties");
		pro.load(reader);
		String logcost = pro.getProperty("logcost");
		String cost = CryptoUtils.decryptAES(logcost, "ossjk");
		// logger.info("LogCostInterceptor---cost---" + cost);
		int i = CommonUtil.diff(CommonMethod.isoFormat.parse(cost), new Date(), "DAYS");
		// System.out.println(i);
		if (i <= -30) {
			httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/toLogin");
			return false;
		}
		// System.out.println(logcost);
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
	}
}